package cn.edu.zjicm.wordsnet_d.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.ExamRunActivity;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.data.test.selection.AffixTest;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;

public class ExamRunMode10Fragment extends Fragment implements OnClickListener {

	public ExamRunMode10Fragment(OneWord mOneWord, ExamRunActivity examRunActivity) {
		this.mOneWord = mOneWord;
		this.mExamRunActivity = examRunActivity;
	}

	public ExamRunMode10Fragment() {

	}

	private ExamRunActivity mExamRunActivity;
	// Data
	private OneWord mOneWord;

	// View
	private TextView mWordText;
	// private TextView mWordPhonetic;
	// private TextView mWordCn;
	private TextView mWordAff;

	private LinearLayout radioLayout[] = new LinearLayout[4];
	private TextView radioText[] = new TextView[4];
	private TextView radioButton[] = new TextView[4];
	private ImageView resultImage[] = new ImageView[4];
	//
	private Button nextButton;
	private ViewFlipper displayFlipper, buttonFlipper;

	private AffixTest affixTestMaker;
	private int[] testBgs;

	int optionABCD; // 随机设置选项A,B,C,D
	boolean everforget;

	Handler handler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// return null;
		View v = inflater.inflate(R.layout.fragment_exam_run_mode10, null);
		mWordText = (TextView) v.findViewById(R.id.word_detail_word);
		mWordAff = (TextView) v.findViewById(R.id.word_aff);
		nextButton = (Button) v.findViewById(R.id.test_next_button);

		displayFlipper = (ViewFlipper) v.findViewById(R.id.diplay_flipper);
		buttonFlipper = (ViewFlipper) v.findViewById(R.id.button_flipper);

		radioLayout[0] = (LinearLayout) v.findViewById(R.id.test_answer_a);
		radioLayout[1] = (LinearLayout) v.findViewById(R.id.test_answer_b);
		radioLayout[2] = (LinearLayout) v.findViewById(R.id.test_answer_c);
		radioLayout[3] = (LinearLayout) v.findViewById(R.id.test_answer_d);
		radioText[0] = (TextView) v.findViewById(R.id.radio_text_num1);
		radioText[1] = (TextView) v.findViewById(R.id.radio_text_num2);
		radioText[2] = (TextView) v.findViewById(R.id.radio_text_num3);
		radioText[3] = (TextView) v.findViewById(R.id.radio_text_num4);
		radioButton[0] = (TextView) v.findViewById(R.id.radio_button_num1);
		radioButton[1] = (TextView) v.findViewById(R.id.radio_button_num2);
		radioButton[2] = (TextView) v.findViewById(R.id.radio_button_num3);
		radioButton[3] = (TextView) v.findViewById(R.id.radio_button_num4);
		resultImage[0] = (ImageView) v.findViewById(R.id.answer_image_a);
		resultImage[1] = (ImageView) v.findViewById(R.id.answer_image_b);
		resultImage[2] = (ImageView) v.findViewById(R.id.answer_image_c);
		resultImage[3] = (ImageView) v.findViewById(R.id.answer_image_d);

		affixTestMaker = new AffixTest(getActivity());
		testBgs = new int[3];
		if (NightModeUtil.isNightModeNow()) {
			testBgs[0] = R.drawable.rectangle_test_bg1;
			testBgs[1] = R.drawable.rectangle_test_bg2;
			testBgs[2] = R.drawable.rectangle_test_bg3;
		} else {
			testBgs[0] = R.drawable.test_bg1;
			testBgs[1] = R.drawable.test_bg2;
			testBgs[2] = R.drawable.test_bg3;
		}

		if (mOneWord != null)
			initView();
		return v;
	}

	public void initViewForNewRelationShip(OneWord mOneWord) {
		this.mOneWord = mOneWord;
		initView();
	}

	private void initView() {
		buttonFlipper.setDisplayedChild(0);
		displayFlipper.setDisplayedChild(0);

		nextButton.setOnClickListener(this);

		for (int i = 0; i < radioButton.length; i++) {
			radioLayout[i].setBackgroundResource(testBgs[0]);
			radioButton[i].setOnClickListener(this);
			radioButton[i].setTextColor(getResources().getColor(R.color.black));
			radioText[i].setTextColor(getResources().getColor(R.color.black));
		}

		everforget = false;
		String q = affixTestMaker.getQuestion(mOneWord);
		mWordAff.setText(q);
		mWordText.setText(mOneWord.getLemma());
		optionABCD = randomNumBtList(4);

		for (int i = 0; i < 4; i++) {
			String text = "";
			if (i == optionABCD) {
				text = affixTestMaker.getRightChoice(mOneWord);
			} else {
				text = affixTestMaker.getWrongChoice(mOneWord);
			}

			radioButton[i].setText(text);
			radioButton[i].setEnabled(true);
			resultImage[i].setBackgroundDrawable(null);
		}
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view == nextButton) {
			if (everforget)
				mExamRunActivity.next(false);
			else
				mExamRunActivity.next(true);
		} else if ((view == radioButton[0]) || (view == radioButton[1]) || (view == radioButton[2]) || (view == radioButton[3])) {
			for (int i = 0; i < radioButton.length; i++)
				if (view == radioButton[i]) {
					if (i == optionABCD) {
						resultImage[i].setBackgroundResource(R.drawable.right_ico);
						radioLayout[i].setBackgroundResource(testBgs[1]);
						radioButton[i].setTextColor(getResources().getColor(R.color.green));
						radioText[i].setTextColor(getResources().getColor(R.color.green));
					} else {
						resultImage[i].setBackgroundResource(R.drawable.wrong_ico);
						radioLayout[i].setBackgroundResource(testBgs[2]);
						radioButton[i].setTextColor(getResources().getColor(R.color.red));
						radioText[i].setTextColor(getResources().getColor(R.color.red));
					}
					break;
				}

			if (view == radioButton[optionABCD]) {
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						displayFlipper.setDisplayedChild(1);
						buttonFlipper.setDisplayedChild(1);

						mWordAff.setText(mOneWord.getAff_content());

						for (int i = 0; i < radioButton.length; i++)
							radioButton[i].setEnabled(false);
					}

				}, 0);

			} else {
				everforget = true;
			}
		}
	}

	private static int randomNumBtList(int length) {
		int index = 0;
		index = (int) (Math.random() * (length));
		return index;
	}
}